import pymysql as sql
from flask import jsonify

connect = sql.Connect(
    host='rm-bp131vjnd9b99vmuq2o.mysql.rds.aliyuncs.com',
    port=3306,
    user='lin_432718',
    passwd='Lin817818',
    db='rail_traffic',
    charset='utf8'
)
cursor = connect.cursor()


def hello():
    print('subcompany operation')


def getAllaccType():
    print('return all acc_type')

    sql = "SELECT * FROM accident_type"
    print(sql)
    cursor.execute(sql)
    temp = cursor.fetchall()
    print(temp)
    if temp == ():
        print("没有结果")
        return 0
    else:
        print("查询成功")
        return temp


def newacc(name, describe,method):
    print('insert a new acc')

    sql = "insert into accident_type values( '" + name + "', '" + describe + "','" + method + "')"
    print(sql)
    try:
        cursor.execute(sql)
        connect.commit()
    except:
        return False
    else:
        return True


def editsub(name, describe, method):
    print('edit a new subcompany')
    sql = "update `accident_type` set `method`='" + method + "',`describe`='" + describe + "' where (`name`='" + name +"')"
    print(sql)
    try:
        cursor.execute(sql)
        connect.commit()
    except:
        return False
    else:
        return True


def searchaccType(word):
    sql = "SELECT * FROM accident_type WHERE name LIKE '%" + word + "%'"
    print(sql)
    cursor.execute(sql)

    temp = cursor.fetchall()
    print(temp)
    if temp == ():
        print("没有符合条件的事故类型")
        return 0
    else:
        print("搜索成功")
        return temp


def deleteaccType(word):
    sql = "Delete FROM accident_type WHERE name='" + str(word)+"'"
    print(sql)
    try:
        cursor.execute(sql)
        connect.commit()
    except:
        return False
    else:
        return True


